<?php
namespace app\jiangnan\controller;
use think\Db;
use think\Controller;
use think\Log;

class User extends Controller
{
    public function bindRoom(){

    }
	public function my2(){
		return $this->fetch();
	}
	public function my(){
		$user_id=request()->get("user_id");
		if(strlen($user_id)!="16"){
			return "错误";
		}
		$this->assign("user_id",$user_id);
		$roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
		$carlist=Db::table("car_owner_welfare")->where("room_id",$roominfo["id"])->select();
		$car_num=Db::table("car_owner_welfare")->where("room_id",$roominfo["id"])->count();
		$this->assign("carlist",$carlist);
		$this->assign("car_num",$car_num);

		//查询福利车订单
		$orderlist=Db::table("car_order_welfare")->where("room_id",$roominfo["id"])->order("id desc")->select();
		$this->assign("orderlist",$orderlist);

		//查询已经使用时长
		//根据房间查询福利车是否到期
		$year=date("Y");
		$month=date("m");
		$halfyear=1;
		if($month>6){
			$halfyear=2;
		}
		$timespan=Db::table("car_order_welfare")
						->where("room_id",$roominfo["id"])
						->where("in_year",$year)
						->where("halfyear",$halfyear)
						->where("pstatus",1)
						->sum("fee_duration");
		$hours=round($timespan/3600,2);
		$lave_hours=30-$hours;
		$this->assign("hours",$hours);
		$this->assign("lave_hours",$lave_hours);
		return $this->fetch();
	}

	public function roominfo(){
    	$user_id=request()->post("user_id");
    	$roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
    	$address="";
    	if(!empty($roominfo)){
    		$address=$roominfo["group"].$roominfo["building"]."幢".$roominfo["unit"]."单元".$roominfo["room"]."室";
    	}
    	return json_encode($address);
    }
    //访客车辆
    public function carlist(){
    	$user_id=request()->get("user_id");
    	if(strlen($user_id)!="16"){
    		return "您还没有绑定房间";
    	}
    	$roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
    	if(empty($roominfo)){
    		return "您还没有绑定房间";
    	}
    	$this->assign("user_id",$user_id);
    	$this->assign("roominfo",$roominfo);
    	$carlist=Db::table("car_owner_welfare")->where("room_id",$roominfo["id"])->select();
    	$this->assign("carlist",$carlist);
    	return $this->fetch();
    }
	public function caradd(){
		if(request()->isPost()){
			$data=request()->post();
			$roominfo=Db::table("roominfo")->where("user_id",$data["user_id"])->find();
			$carinfo=Db::table("car_owner_welfare")->where("out_parking_id",$roominfo["out_parking_id"])->where("car_number",$data["car_number"])->find();
			if(!empty($carinfo)){
				return json(['error' => '该车牌已经添加，不能重复添加']);
			}else{
				$info["out_parking_id"]=$roominfo["out_parking_id"];
				$info["parking_name"]=$roominfo["parking_name"];
				$info["owner_name"]=$roominfo["owner_name"];
				$info["owner_phone"]=$roominfo["phone"];
				$info["address"]=$roominfo["group"].$roominfo["building"]."幢".$roominfo["unit"]."单元".$roominfo["room"]."室";
				$info["create_time"]=time();
				$info["room_id"]=$roominfo["id"];

				$info["car_number"]=$data["car_number"];
				$info["operator"]=$data["user_id"];
				$info["name"]=$data["name"];
				$info["phone"]=$data["phone"];
				$res=Db::table("car_owner_welfare")->insert($info);
				if($res){
					return json(['success' => '车牌添加成功']);
				}else{
					return json(['error' => '车牌添加失败']);
				}
			}
		}else{
			$user_id=request()->get("user_id");
			$this->assign("user_id",$user_id);
			return $this->fetch();
		}
	}

	public function cardel(){
		$id=request()->post("id");
		$user_id=request()->post("user_id");
		$carinfo=Db::table("car_owner_welfare")
					->where("id",$id)
					->find();
		if(empty($carinfo)){
			return json(['error' => '车辆信息不存在']);
		} 
		$res=Db::table("car_owner_welfare")->where("id",$id)->delete();
		if($res){
			$data=$carinfo;
			$data["action"]=2;
			$data["operate_time"]=time();
			$data["operate_name"]=$user_id;
			Db::table("car_owner_welfare_operate")->insert($data);
			return json(['error' => '车辆删除成功']);
		}else{
			return json(['error' => '车辆信息不存在']);
		}
	}
	//福利车订单
	public function orderlist(){
		$user_id=request()->get("user_id");
    	if(strlen($user_id)!="16"){
    		return "您还没有绑定房间";
    	}
    	return 2;
	}

	//业主车辆添加
	public function carLibraryList(){
	    return ;
		$user_id=request()->get("user_id");
		if(strlen($user_id)!="16"){
    		return "您还没有绑定房间";
    	}
    	$this->assign("user_id",$user_id);
    	$roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
    	$this->assign("roominfo",$roominfo);
    	$library=Db::table("library")->where("room_id",$roominfo["id"])->find();
    	$carlist=array();
    	if(!empty($library)){
    		$carlist=explode(",",$library["car_number"]);
    	}
    	$num=count($carlist);
    	$this->assign("num",$num);
    	$this->assign("library",$library);
    	$this->assign("carlist",$carlist);
    	return $this->fetch("car_library_list");
	}

	public function carLibraryAdd(){
		if(request()->isPost()){
			$data=request()->post();
			$roominfo=Db::table("roominfo")->where("user_id",$data["user_id"])->find();
			$library=Db::table("library")
						->where("room_id",$roominfo["id"])
						->where("user_id",$data["user_id"])
						->find();
			if(!empty($library)){
				$car_number=$library["car_number"].",".$data["car_number"];
				$res=Db::table("library")->where("id",$library["id"])->update(["car_number"=>$car_number]);
				if($res){
					return json(['success' => '车牌添加成功']);
				}else{
					return json(['error' => '车牌添加失败']);
				}
			}else{
				$info["out_parking_id"]=$roominfo["out_parking_id"];
				$info["parking_name"]=$roominfo["parking_name"];
				$info["group"]=$roominfo["group"];
				$info["building"]=$roominfo["building"];
				$info["unit"]=$roominfo["unit"];
				$info["room"]=$roominfo["room"];
				$info["room_id"]=$roominfo["id"];
				
				$info["community_name"]=$roominfo["community_name"];;
				
				$info["car_number"]=$data["car_number"];
				$info["user_id"]=$data["user_id"];
				$info["status"]=1;
				$info["in_time"]=time();
				$info["out_time"]=strtotime("2020-12-31 23:59:00");
				$info["name"]=$roominfo["owner_name"];
				$info["phone"]=$roominfo["phone"];
				$res=Db::table("library")->insert($info);
				if($res){
					return json(['success' => '车牌添加成功']);
				}else{
					return json(['error' => '车牌添加失败']);
				}
			}
		}else{
			$user_id=request()->get("user_id");
			$this->assign("user_id",$user_id);
			return $this->fetch("car_library_add");
		}
	}
    

    public function violation(){
    	if(request()->isPost()){

    	}else{
    		$user_id=request()->get("user_id");
    		return $this->fetch();
    	}
    	
    }
}